package com.zqcy.workbench.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;

import com.onemdos.banner.OneMDOSBanner;
import com.onemdos.banner.OneMDOSBannerAction;
import com.onemdos.banner.listener.OneMDOSBannerSplashListener;
import com.onemdos.banner.protocol.advertsrv.ActivityMaterial;
import com.onemdos.storage.kv.MDOSPreferencesManager;
import com.shinemo.minisdk.util.ColorUtils;
import com.zqcy.workbench.databinding.ActivitySplashBinding;
import com.zqcy.workbench.utils.Constants;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * 闪屏页
 */
public class SplashActivity extends AppCompatActivity {

    private ActivitySplashBinding mSplashBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSplashBinding = ActivitySplashBinding.inflate(LayoutInflater.from(this));
        setContentView(mSplashBinding.getRoot());
        //设置全屏模式
        ColorUtils.setTransparent(this, Constants.isDarkModel,1);

        new OneMDOSBannerAction(SplashActivity.this)
                .setShowTimeLong(3000)
                .enableSkip(true)
                .requestScreenSplash("STARTPAGE", new OneMDOSBannerSplashListener() {
                    @Override
                    public void noCache() {
                        //开屏广告无缓存，可自定义做想做的事情
                        Log.e("tag", "no cache");
                        inItSplash();
                    }
                });
    }

    private void inItSplash() {
        if (isDestroyed()) return;
        //oneaccess闪退，等适配好打开
        if (MDOSPreferencesManager.getInstance(SplashActivity.this, Constants.APP_SP_NAME).getBoolean(Constants.IS_LOGIN, false)) {
            MainActivity.startActivity(SplashActivity.this);
        } else {
            LoginActivity.startActivity(SplashActivity.this);
        }
//        MainActivity.startActivity(SplashActivity.this);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (resultCode == OneMDOSBanner.SplashCloseResultCode) {
                if (data != null) {
                    //获取的开屏页对象数据
                    ActivityMaterial activityMaterial = (ActivityMaterial) data.getSerializableExtra(OneMDOSBanner.SPLASH_RESULT);

                }
                inItSplash();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}